# Experiment 2, additional tables and figures from the appendix

###
# Figure 7
###
#```{r levels figure,fig.cap="\\label{fig:levels}Levels of preferred policy response by treatment condition. Graphs in columns are subsetted to either a Labor shock or an Automation shock treatment. Vertical lines represent the mean response by treatment condition."}

#Summary plot

p1<-plotter(subsample = "Labor",depvar="restrict_imports")+labs(title="Within Labor Treatment")
p2<-plotter(subsample = "Labor",depvar="benefits_to_unemployed")
p3<-plotter(subsample = "Automation",depvar="restrict_automation")+labs(title="Within Automation Treatment")
p4<-plotter(subsample = "Automation",depvar="benefits_to_unemployed")

#Code inspired by https://wilkelab.org/cowplot/articles/shared_legends.html

legend <- cowplot::get_legend(
  
  p1 + 
    guides(color = guide_legend(title="Treatment",nrow = 1)) +
    theme(legend.position = "bottom")
)

combined.plot<-cowplot::plot_grid(
  p1+coord_cartesian(xlim=c(30,70)),
  p3+coord_cartesian(xlim=c(30,70)),
  p2+coord_cartesian(xlim=c(30,70)),
  p4+coord_cartesian(xlim=c(30,70)),
  nrow=2,
  ncol=2
)

cowplot::plot_grid(combined.plot,legend,ncol=1,rel_heights = c(2,0.25))


###
# Figure 8
###
#```{r testing for mismatched policies,fig.cap="\\label{fig:mismatched}Differences of preferred policy response from transfers by treatment condition. Graphs in columns are subsetted to either a Labor shock or an Automation shock treatment. Vertical lines represent the mean response by treatment condition."}

p1.mismatch<-plotter(subsample="Labor",depvar="restrict_automation_difference",return_df = FALSE)+labs(title="Subsample: Labor")
p2.mismatch<-plotter(subsample="Labor",depvar="restrict_imports_difference",return_df = FALSE)+labs(title="Subsample: Labor")
p3.mismatch<-plotter(subsample="Automation",depvar="restrict_automation_difference",return_df = FALSE)+labs(title="Subsample: Automation")
p4.mismatch<-plotter(subsample="Automation",depvar="restrict_imports_difference",return_df = FALSE)+labs(title="Subsample: Automation")

#Code inspired by https://wilkelab.org/cowplot/articles/shared_legends.html

legend <- cowplot::get_legend(
  
  p1.mismatch + 
    guides(color = guide_legend(title="Treatment",nrow = 1)) +
    theme(legend.position = "bottom")
)

combined.plot.mismatch<-cowplot::plot_grid(
  p1.mismatch+coord_cartesian(xlim=c(-25,25)),
  p3.mismatch+coord_cartesian(xlim=c(-25,25)),
  p2.mismatch+coord_cartesian(xlim=c(-25,25)),
  p4.mismatch+coord_cartesian(xlim=c(-25,25)),
  nrow=2,
  ncol=2
)

cowplot::plot_grid(combined.plot.mismatch,legend,ncol=1,rel_heights = c(2,0.25))




###
# Table 7
###
#```{r regressions on differences with dummies da base long control list ,eval=TRUE,results='asis'}

run_analysis<-function(depvar,controls=FALSE,subsample=TRUE,return_lm_robust=TRUE){
  
  if(controls==TRUE){
    control_str<-balance_controls_vec
  } else if(class(controls) == "character"){
    control_str<-controls
  } else {
    control_str<-NULL
  }
  
  fmla<-as.formula(paste(c(paste0(depvar, " ~ treatment_fdal_dabase + sample_date"), control_str), collapse="+"))
  
  ols<-lm(fmla,data=filter(lucid.proc,{{subsample}}))
  
  return(ols)
  
}

long_controls_vec<-c("education_str","gender_str","hispanic_str","region_str","ethnicity_str","hhi_str","party_dem","party_rep")
long_controls<-paste(c("",long_controls_vec),collapse=" + ")

lucid.proc$treatment_fdal_dabase = relevel(lucid.proc$treatment_fdal, ref=2)

main_models_long<-list(
  run_analysis(depvar="restrict_imports_difference",subsample=treatment_fdal_dabase %in% c("Domestic Automation","Foreign Labor")),
  run_analysis(depvar="restrict_imports_difference",controls=long_controls,subsample=treatment_fdal_dabase %in% c("Domestic Automation","Foreign Labor")),
  run_analysis(depvar="restrict_automation_difference",subsample=treatment_fdal_dabase %in% c("Domestic Automation","Foreign Automation")),
  run_analysis(depvar="restrict_automation_difference",controls = long_controls,subsample=treatment_fdal_dabase %in% c("Domestic Automation","Foreign Automation"))
)

stargazer::stargazer(
  main_models_long,
  se=estimatr::starprep(main_models_long,se_type="HC1"),
  p=estimatr::starprep(main_models_long,stat="p.value",se_type="HC1"),
  add.lines = list(c("Controls","No","Yes","No","Yes"),c("Subsample","DA + FL","DA + FL","DA + FA","DA + FA")),
  keep="treatment|sample",
  dep.var.labels=c(
    "\\makecell{restrict \\\\ imports \\\\ difference}",
    "\\makecell{restrict \\\\ automation \\\\ difference}"
  ),
  keep.stat="n", 
  covariate.labels=c("Foreign Labor","Foreign Automation","Sept Sample"),
  header = FALSE,
  title="Main results with long control list",
  label="tab:favsdareglong", no.space=TRUE,
  type="latex"
)




###
# Table 8
###

lucid.proc$treatment_fdal_dabase = relevel(lucid.proc$treatment_fdal, ref=2)

main_models_mismatched<-list(
  run_analysis(depvar="restrict_imports_difference",subsample=treatment_fdal_dabase %in% c("Domestic Automation","Foreign Labor")),
  run_analysis(depvar="restrict_automation_difference",subsample=treatment_fdal_dabase %in% c("Domestic Automation","Foreign Labor")),
  run_analysis(depvar="restrict_imports_difference",subsample=treatment_fdal_dabase %in% c("Domestic Automation","Foreign Automation")),
  run_analysis(depvar="restrict_automation_difference",subsample=treatment_fdal_dabase %in% c("Domestic Automation","Foreign Automation"))
)

stargazer::stargazer(
  main_models_mismatched,
  se=estimatr::starprep(main_models_mismatched,se_type="HC1"),
  p=estimatr::starprep(main_models_mismatched,stat="p.value",se_type="HC1"),
  add.lines = list(c("Controls","No","No","No","No"),c("Subsample","DA + FL","DA + FL","DA + FA","DA + FA")),
  keep="treatment|sample",
  dep.var.labels=c(
    "\\makecell{restrict \\\\ imports \\\\ difference}",
    "\\makecell{restrict \\\\ automation \\\\ difference}",
    "\\makecell{restrict \\\\ imports \\\\ difference}",
    "\\makecell{restrict \\\\ automation \\\\ difference}"
  ),
  keep.stat="n", 
  covariate.labels=c("Foreign Labor","Foreign Automation","Sept Sample"),
  header = FALSE,
  title="Main results with mismatched policies",
  label="tab:favsdaregmismatch", no.space=TRUE,
  type="latex"
)


###
# Table 12
###

lucid.proc$policy_difference <- ifelse(lucid.proc$treatment_automation_labor=="Labor",lucid.proc$restrict_imports_difference,lucid.proc$restrict_automation_difference)

#```{r main specifications flda race interactions bp and difference, eval=TRUE,results='asis'}
#library(stargazer)
#library(estimatr)

lucid.proc <- lucid.proc %>% 
  mutate(
    relevant_policy=case_when(
      treatment_automation_labor=="Labor" ~ restrict_imports,
      treatment_automation_labor=="Automation" ~ restrict_automation,
      TRUE ~ NA_real_
    )
  )

controls_vec_race <- c("educ_nodegree","educ_ba","educ_advdegree","male","hispanicbin","region","white","black","hispanicbin","hhi_num","party_dem","party_rep")

#bpm2a <- lm(relevant_policy ~ treatment_fdal_dabase*white + sample_date, data=subset(lucid.proc, (treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Labor")))
bpm2b <- lm(paste0("relevant_policy ~ treatment_fdal_dabase*white + sample_date +",paste(controls_vec_race,collapse=" + ")),data=subset(lucid.proc, treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Labor"))

bpm2c <- lm(paste0("relevant_policy ~ treatment_fdal_dabase*white + sample_date +",paste(controls_vec_race,collapse=" + ")),data=subset(lucid.proc, treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Automation"))


#diffm2a <- lm(policy_difference ~ treatment_fdal_dabase*white + sample_date, data=subset(lucid.proc, (treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Labor")))
diffm2b <- lm(paste0("policy_difference ~ treatment_fdal_dabase*white + sample_date +",paste(controls_vec_race,collapse=" + ")),data=subset(lucid.proc, treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Labor"))

diffm2c <- lm(paste0("policy_difference ~ treatment_fdal_dabase*white + sample_date +",paste(controls_vec_race,collapse=" + ")),data=subset(lucid.proc, treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Automation"))

stargazer(bpm2b,diffm2b,bpm2c,diffm2c,  se = starprep(bpm2b, diffm2b, bpm2c, diffm2c),
          keep.stat="n", header = FALSE,
          title="Effect of Treatment on Backpedaling Policy and Differences",
          label = "tab:bpdiff",
          omit.table.layout ="dl",
          keep = c("treatment_fdal_dabaseForeign Labor","treatment_fdal_dabaseForeign Automation","whiteTRUE","treatment_fdal_dabaseForeign Labor:whiteTRUE","Constant"),
          column.labels=c("Backpedal","Difference", "Reg. Auto.","Difference"),
          covariate.labels=c("For. Lab.","For. Auto.","White","White*For. Lab.","White*For. Auto."),
          #          add.lines=list(c("Controls?", "N","Y","N","Y")),
          no.space=TRUE)




###
# Table 13
###

#```{r main specifications flda race interactions bp and difference no controls, eval=TRUE,results='asis'}
#library(stargazer)
#library(estimatr)

lucid.proc <- lucid.proc %>% 
  mutate(
    relevant_policy=case_when(
      treatment_automation_labor=="Labor" ~ restrict_imports,
      treatment_automation_labor=="Automation" ~ restrict_automation,
      TRUE ~ NA_real_
    )
  )


bpm2a <- lm(relevant_policy ~ treatment_fdal_dabase*white + sample_date, data=subset(lucid.proc, (treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Labor")))

bpm2d <- lm(relevant_policy ~ treatment_fdal_dabase*white + sample_date, data=subset(lucid.proc, (treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Automation")))

diffm2a <- lm(policy_difference ~ treatment_fdal_dabase*white + sample_date, data=subset(lucid.proc, (treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Labor")))

diffm2d <- lm(policy_difference ~ treatment_fdal_dabase*white + sample_date, data=subset(lucid.proc, (treatment_fdal_dabase == "Domestic Automation" | treatment_fdal_dabase == "Foreign Automation")))

stargazer(bpm2a,diffm2a,bpm2d,diffm2d,  se = starprep(bpm2a, diffm2a,bpm2d,diffm2d),
          keep.stat="n", header = FALSE,
          title="Effect of Treatment on Backpedaling Policy and Differences, no controls",
          label = "tab:bpdiffnoctrl",
          omit.table.layout ="dl",
          keep = c("treatment_fdal_dabaseForeign Labor","treatment_fdal_dabaseForeign Automation","whiteTRUE","treatment_fdal_dabaseForeign Labor:whiteTRUE","Constant"),
          column.labels=c("Backpedal","Difference", "Reg. Auto.","Difference"),
          covariate.labels=c("For. Lab.","For. Auto.","White","White*For. Lab.","White*For. Auto."),
          #          add.lines=list(c("Controls?", "N","Y","N","Y")),
          no.space=TRUE)
